package nio.book;


import java.io.InputStream;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.ByteBuffer;
import java.nio.channels.*;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Iterator;
import java.util.Set;

/**
 * SocketChannel 套字节连接
 */
public class NioDemo2 {

    public static void main(String[] args) throws Exception {

        if ("1".equals(args[0])) {
            client();
        } else
            discardServer();
    }

    public static void client() throws Exception {
        SocketChannel client = SocketChannel.open();
        client.configureBlocking(false);

        client.connect(new InetSocketAddress("127.0.0.1", 7788));

        while (!client.finishConnect()) {
            System.out.println("等待连接..........");
            Thread.sleep(1000);
        }

        ByteBuffer buf = ByteBuffer.allocate(1024);

        buf.put("abc".getBytes(StandardCharsets.UTF_8));

        buf.flip();

        client.write(buf);

        client.finishConnect();
        client.close();
    }

    public static void server() throws Exception {
        ServerSocketChannel server = ServerSocketChannel.open();

        server.configureBlocking(true);

        ServerSocket socket = server.socket();

        socket.bind(new InetSocketAddress(7788));

        Socket accept = socket.accept();


//        InputStream stream = accept.getInputStream();


//        byte[] b = new byte[1024];
//        int read = stream.read(b);

//        System.out.println(new String(b,StandardCharsets.UTF_8));
        ByteBuffer buf = ByteBuffer.allocate(1024);
        SocketChannel channel = accept.getChannel();
        int read = channel.read(buf);

        System.out.println(read);
    }


    public static void discardServer() throws Exception {
        ServerSocketChannel server = ServerSocketChannel.open();
        server.bind(new InetSocketAddress(7788));
        server.configureBlocking(false);

        ByteBuffer buf = ByteBuffer.allocate(1024);


        Selector selector = Selector.open();
        server.register(selector, SelectionKey.OP_ACCEPT);

        while (selector.select() > 0) {
            Set<SelectionKey> keys = selector.selectedKeys();

            Iterator<SelectionKey> iterator = keys.iterator();

            while (iterator.hasNext()) {
                SelectionKey key = iterator.next();
                if (key.isAcceptable()) {
                    SocketChannel socketChannel = server.accept();
                    socketChannel.configureBlocking(false);
                    socketChannel.register(selector, SelectionKey.OP_READ);
                } else if (key.isReadable()) {
                    SocketChannel channel = (SocketChannel) key.channel();

                    int read = channel.read(buf);
                    System.out.println(read);
                    buf.flip();
                    System.out.println(new String(buf.array()));
                    channel.close();
                }
                iterator.remove();
            }
        }


    }
}
